import 'package:flutter/material.dart';
import 'package:image_pickers/image_pickers.dart';
import 'package:top_foundation/utils/theme/button.dart';
import 'package:top_foundation/utils/theme/theme.dart';
import 'package:whale/helper/utils/media_picker.dart';
import 'package:whale/helper/utils/util.dart';

class AddVideoView extends StatefulWidget {
  const AddVideoView({super.key});

  @override
  State<AddVideoView> createState() => _AddVideoViewState();
}

class _AddVideoViewState extends State<AddVideoView> {
  void _pick() async {
    final medias = await MediaPicker.pickImages(galleryMode: GalleryMode.video);
    list.addAll(medias);
    setState(() {});
  }

  void _gallery(int index) {
    MediaPicker.previewMedias(initIndex: index, imageMedias: list);
  }

  late List<Media> list = [];
  @override
  Widget build(BuildContext context) {
    return Container(
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          IButton.text(
              onPressed: _pick,
              text: Ls.uploadvideo,
              textStyle: TextStyle(
                color: ITheme.selectedColor,
                fontSize: 15.sp,
              )),
          ...List.generate(list.length, (index) {
            final m = list[index];
            return Container(
              height: 40.r,
              child: Row(
                children: [
                  Expanded(
                    child: IButton.text(
                        alignment: Alignment.centerLeft,
                        onPressed: () {
                          _gallery(index);
                        },
                        text: Uri.file(m.path!).pathSegments.last,
                        textStyle: TextStyle(
                            color: ITheme.selectedColor, fontSize: 13.sp)),
                  ),
                  IButton.icon(
                      onPressed: () {
                        list.removeAt(index);
                        setState(() {});
                      },
                      icon: Icon(
                        Icons.delete_rounded,
                        color: Colors.red,
                        size: 25.r,
                      ))
                ],
              ),
            );
          })
        ],
      ),
    );
  }
}
